import 'package:flutter/material.dart';
import '../pages/tabs.dart';
import '../pages/NameRouter/news.dart';
import '../pages/NameRouter/form.dart';
import '../pages/NameRouter/category.dart';
import '../pages/NameRouter/shop.dart';

//1.配置路由
final Map _routers = {
  '/': (context) => MyTabs(),
  '/news': (context) => NewsPage(),
  '/category': (context) => CategoryPage(),
  '/shop': (context, {arguments}) => ShopPage(arguments: arguments),
  '/form': (context, {arguments}) => FormPage(arguments: arguments),
};

// 2.配置onGenerateRoute 固定写法  相当于一个中间件 可以做权限判断
var onGenerateRoute = (RouteSettings settings) {
  // print(settings);
  // print(settings.name);
  // print(settings.arguments);

  // 统一处理
  final String? name = settings.name; //news 或者是 form  -- 命名路由名称
  final Function? pageContentBuilder =
      _routers[name]; //(context) => MyTabs(),  router里面的Funtion

  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
        builder: (context) =>
            pageContentBuilder(context, arguments: settings.arguments),
      );
      return route;
    } else {
      final Route route = MaterialPageRoute(
        builder: (context) => pageContentBuilder(context),
      );
      return route;
    }
  }
  return null;
};
